﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using System.IO;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// FileSelectorDialog.xaml 的交互逻辑
    /// </summary>
    public partial class FileSelectorDialog : MetroWindow
    {
        public FileSelectorDialog(string[] fullPathOfFiles)
        {
            InitializeComponent();

            if (fullPathOfFiles == null || fullPathOfFiles.Length <= 0) return;

            foreach (var path in fullPathOfFiles)
            {
                if (System.IO.File.Exists(path))
                {
                    var fsi = new FileSelectorItem(path);
                    lstFiles.Items.Add(fsi);
                }
            }
        }

        private List<string> selectedFilePaths = null;

        public List<string> SelectedFilePaths { get { return this.selectedFilePaths; } }

        private void btnCancle_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            selectedFilePaths = new List<string>();

            foreach (var item in lstFiles.Items)
            {
                var fsi = item as FileSelectorItem;
                if (fsi == null) continue;

                if (fsi.IsChecked == true) selectedFilePaths.Add(fsi.FilePath);
            }

            this.DialogResult = true;
            this.Close();
        }

        private void btnSelectAll_Click(object sender, RoutedEventArgs e)
        {
            foreach (var i in lstFiles.Items)
            {
                var fsi = i as FileSelectorItem;
                if (fsi != null)
                {
                    fsi.IsChecked = true;
                }
            }
        }

        private void btnUnselectAll_Click(object sender, RoutedEventArgs e)
        {
            foreach (var i in lstFiles.Items)
            {
                var fsi = i as FileSelectorItem;
                if (fsi != null)
                {
                    fsi.IsChecked = false;
                }
            }
        }

        private void btnSwitchSelect_Click(object sender, RoutedEventArgs e)
        {
            foreach (var i in lstFiles.Items)
            {
                var fsi = i as FileSelectorItem;
                if (fsi != null)
                {
                    if (fsi.IsChecked == true)
                    {
                        fsi.IsChecked = false;
                    }
                    else fsi.IsChecked = true;
                }
            }
        }
    }

    public class FileSelectorItem : ListBoxItem
    {
        public FileSelectorItem(string filePath)
        {
            this.HorizontalAlignment = HorizontalAlignment.Center;
            var grid = new Grid();
            grid.RowDefinitions.Add(new RowDefinition());
            grid.RowDefinitions.Add(new RowDefinition());

            var title = MainWindow.GetTitleOfMdFile(filePath);
            if (string.IsNullOrWhiteSpace(title))
            {
                ckx.Content = new FileInfo(filePath).Name;
            }
            else ckx.Content = title;

            grid.Children.Add(ckx);

            this.filePath = filePath;

            var tb = new TextBlock()
            {
                Text = "> " + filePath,
                Margin = textBlockMargin,
                Width = 400,
                TextWrapping = TextWrapping.Wrap,
                Foreground = textColor,
            };
            Grid.SetRow(tb, 1);
            grid.Children.Add(tb);
            this.Content = new Border()
            {
                Child = grid,
                BorderBrush = Brushes.SaddleBrown,
                BorderThickness = new Thickness(0, 0, 0, 1),
                SnapsToDevicePixels = true,
                Padding = padding,
                Margin = marging,
            };

            this.PreviewMouseLeftButtonDown += FileSelectorItem_PreviewMouseLeftButtonDown;
            ckx.Checked += Ckx_Checked;
            ckx.Unchecked += Ckx_Unchecked;
        }

        private void Ckx_Unchecked(object sender, RoutedEventArgs e)
        {
            this.Background = Brushes.LightGray;
        }

        private void Ckx_Checked(object sender, RoutedEventArgs e)
        {
            this.Background = Brushes.Transparent;
        }

        private static Brush textColor = new SolidColorBrush(new Color() { R = 187, G = 97, B = 21, A = 255, });

        private static Thickness marging = new Thickness(4, 2, 4, 2);

        private static Thickness padding = new Thickness(2);

        private static Thickness textBlockMargin = new Thickness(30, 2, 10, 2);

        private void FileSelectorItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            if (ckx.IsChecked == true)
            {
                ckx.IsChecked = false;
            }
            else ckx.IsChecked = true;
        }

        private CheckBox ckx = new CheckBox() { Margin = new Thickness(4, 2, 4, 2), IsChecked = true, };

        public bool? IsChecked
        {
            get { return ckx.IsChecked; }
            set { ckx.IsChecked = value; }
        }

        private string filePath = "";

        public string FilePath
        {
            get { return this.filePath; }
        }
    }
}
